/*
 *                  BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 * 
 * Created on Dec 6, 2006
 * 
 */

package org.biojava.services.das.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.biojava.dasobert.dasregistry.DasCoordinateSystem;
import org.biojava.services.das.dao.DasSourceManager;
import org.biojava.services.das.registry.ConfigBean;
import org.biojava.services.das2.Das1SourceDbProvider;
import org.biojava.services.das2.DasSourceProvider;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CoordSysServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	ConfigBean config;
	ApplicationContext context;
	DasSourceManager dao;

	DasSourceProvider das1provider;
	DasSourceProvider das2provider;
	

	public CoordSysServlet() {
	}

	public void init(ServletConfig sconfig) throws ServletException {
		super.init(sconfig);
		ApplicationContext ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
		das1provider = (Das1SourceDbProvider)ctx.getBean("das1SourceDbProvider");
		
	}

	public void destroy() {
		super.destroy();
		// paranoid setting everything to null to avoid memory leaks...
		context = null;
		dao = null;
		config = null;

	}


	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		String cmdPath = req.getPathInfo();
		if (cmdPath == null) {
			cmdPath = "";
		}
	
		StringTokenizer toke = new StringTokenizer(cmdPath, "/");

		String command = toke.nextToken();
		resp.setHeader("Access-Control-Allow-Origin", "*");
		resp.setHeader("Content-Type",ServletResponseWriter.XML_CONTENT_TYPE);
		sendCoordinateSystem(req, resp, command);

	}

	private void sendCoordinateSystem(HttpServletRequest req,
			HttpServletResponse resp, String name) {
		List<DasCoordinateSystem> dcs = das1provider.getCoordinateSystems();
		List<DasCoordinateSystem> coordsToWrite = new ArrayList();

		for (DasCoordinateSystem coord : dcs) {

			if (coord.getUniqueId().equalsIgnoreCase(name)) {
				// / we found it!
				coordsToWrite.add(coord);
			}
		}

		ServletResponseWriter writer = new ServletResponseWriter();
		writer.writeCoordinateSystemResponse(resp, coordsToWrite, 0, 0, 0);
	}

}
